package com.yztstudy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yztstudy.dto.Result;
import com.yztstudy.dto.UserDTO;
import com.yztstudy.entity.Follow;
import com.yztstudy.entity.User;
import com.yztstudy.mapper.FollowMapper;
import com.yztstudy.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yztstudy.service.IUserService;
import com.yztstudy.utils.RedisConstants;
import com.yztstudy.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserService userService;

    /**
     * TODO 关注或者取关用户
     * @param follow_id 被关注的人的ID
     * @param isFollow 是否关注
     * @return
     */
    @Override
    public Result follow(Long follow_id, Boolean isFollow) {

        // TODO 1.获取当前用户
        Long userId = UserHolder.getUser().getId();

        // TODO 2.关注，就新增数据
        if (isFollow)
        {
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(follow_id);
            boolean isSave = save(follow);
            if (isSave)
            {
                stringRedisTemplate.opsForSet().add(RedisConstants.FOLLOWS + userId, follow_id.toString()); //加入redis
            }
        }

        // TODO 3.取消关注，就删除一条数据
        else {
            boolean isRemove = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", follow_id));
            if (isRemove)
            {
                stringRedisTemplate.opsForSet().remove(RedisConstants.FOLLOWS + userId, follow_id.toString()); //从redis中删除
            }
        }

        return Result.ok();
    }


    /**
     * TODO 关注与否
     * @param follow_id 被关注的人的ID
     * @return
     */
    @Override
    public Result followOrNot(Long follow_id) {

        // TODO 1.获取当前用户
        Long userId = UserHolder.getUser().getId();

        // TODO 2.查询是否关注了这个用户
        Long count = query().eq("user_id", userId).eq("follow_user_id", follow_id).count();
        if (count > 0) //关注
        {
            return Result.ok(true);
        }else {
            return Result.ok(false);
        }

    }


    /**
     * TODO 查询共同关注的人
     * @param follow_id 被关注的人ID
     * @return
     */
    @Override
    public Result commonFollow(Long follow_id) {

        // TODO 1.获取当前用户的ID
        Long userId = UserHolder.getUser().getId();

        // TODO 2.求两个set集合的交集
        Set<String> interSection = stringRedisTemplate.opsForSet().intersect(RedisConstants.FOLLOWS + userId.toString(), RedisConstants.FOLLOWS + follow_id.toString());
        if(interSection == null || interSection.isEmpty()) //如果查询结果为空
        {
            return Result.ok(Collections.emptyList());
        }

        // TODO 3.格式转换
        List<Long> IDs = interSection.stream().map(Long::valueOf).collect(Collectors.toList());

        // TODO 4.查询用户
        List<User> users = userService.listByIds(IDs);

        // TODO 5.信息屏蔽
        List<UserDTO> userDTOs = users.stream().map(user -> {
            return BeanUtil.copyProperties(user, UserDTO.class);
        }).collect(Collectors.toList());

        return Result.ok(userDTOs);
    }
}
